//需传参数 fromUserId, nickName
const service = require('./msgDetailsService.js');
const app = getApp();
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalData: {},
    fromUserId: null,
    toUerId: "",
    msgRecords: [], //当前窗口聊天记录
    requestData: {}, //接口返回的其他信息 
    msgContent: "",  //输入的消息内容
    chatRoomId: "",  //房间id
    currentHeight: 0 //存储上一次盒子高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page = 0;      
    var self = this;
    console.log(options.fromUserId, options.nickName)
    this.setData({
      toUerId: options.fromUserId,
      globalData: app.globalData
    })
    console.log(app.globalData)
    //如果有房间id咋获取聊天记录， 没有则根据userId创建房间
    if(options.chatRoomId){
      this.setData({chatRoomId: options.chatRoomId})
      this.requestRecords(function () {
        self.pageScrollToBottom()
      })
    }else {
      service.msgRoomCreate(options.fromUserId, (data) => {
        self.setData({chatRoomId: data.id})
        self.requestRecords(function(){
          self.pageScrollToBottom()
        })
      })
    }
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: options.nickName || '',
    })

  },
  requestRecords: function(cb){
    var self = this;
    service.msgRecordGet(page, this.data.chatRoomId, (data) => {
      let currentData = data.items.reverse().concat(this.data.msgRecords)
      this.setData({msgRecords: currentData, requestData: data})
      typeof cb == "function" && cb()
      self.pageScrollTo()
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      globalData: app.globalData
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  parserDate (date) {  
    var y = date.getFullYear();  
    var m = date.getMonth() + 1;  
    m = m < 10 ? ('0' + m) : m;  
    var d = date.getDate();  
    d = d < 10 ? ('0' + d) : d;  
    var h = date.getHours();  
    h=h < 10 ? ('0' + h) : h;  
    var minute = date.getMinutes();  
    minute = minute < 10 ? ('0' + minute) : minute;  
    var second=date.getSeconds();  
    second=second < 10 ? ('0' + second) : second;  
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;   
  },
  sendBtnClick () { 
    var self = this;
    let content=this.data.msgContent;
    let chatRoomId = this.data.chatRoomId;
    let nowDate = this.parserDate (new Date())
    if(content){
      //发送消息
      service.msgSend(chatRoomId, content, () => {
        //发送成功后更新本地数据
        let newMsg = {
          content: content,
          createTime: nowDate,
          fromUserId: self.data.globalData.userId 
        }
        let items = self.data.msgRecords
        
        items.push(newMsg)
        self.setData({msgContent: '', msgRecords: items})
        
        self.pageScrollToBottom()
      })
    }
  },
  msgContentInput(e){
    this.setData({msgContent: e.detail.value})
  },
  // 下拉刷新
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh()
  },
  upper(){
    if (this.data.requestData.hasNextPage == true) {
      ++page
      this.requestRecords()
    }
  },
  lower(){

  },
  // 滚到指定位置
  pageScrollTo: function(){
    wx.createSelectorQuery().select('#talkBox').boundingClientRect((rect) => {
      console.log(3,rect.height)
      //计算应滚动高度为当前高度减去上一次高度
      let sHeight = rect.height - this.data.currentHeight
      wx.pageScrollTo({
        scrollTop: sHeight || rect.height,
        duration: 0
      })
      // 保存当前盒子高度
      this.setData({currentHeight: rect.height})
      console.log(sHeight)
    }).exec()
  },
  // 滚动到底部
  pageScrollToBottom: function () {
    this.setData({
      scrollTop: 360 * this.data.msgRecords.length
    })
    // 使页面滚动到底部
    wx.pageScrollTo({
      scrollTop: 360 * this.data.msgRecords.length,
      duration: 0
    })
  },
})